//
//  ConsoleView.m
//  ScMix
//
//  Created by apple on 18/10/11.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ConsoleView.h"

@interface ConsoleView ()<NSTextViewDelegate>

@property (nonatomic, strong) NSScrollView *scrollView;
@property (nonatomic, strong) NSTextView *textView;
@end

@implementation ConsoleView

- (instancetype)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadViews];
    }
    return self;
}

- (void)loadViews{
    
    self.textView = [[NSTextView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self addSubview:self.textView];
    self.textView.font = [NSFont fontWithName:@"SF Mono" size:14];
    self.textView.delegate = self;
    [self.textView setEditable:NO];
    //    self.textView.textColor = [NSColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1.0];
    self.textView.textColor = [NSColor blackColor];
    self.scrollView = [[NSScrollView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self.scrollView setBorderType:NSNoBorder];
    [self.scrollView setHasVerticalScroller:YES];
    [self.scrollView setHasHorizontalScroller:NO];
    [self.scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
    self.scrollView.contentView.postsBoundsChangedNotifications = NO;
    
    
    //    [self.textView setMinSize:NSMakeSize(0.0, self.frame.size.height - textViewHeight)];
    [self.textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
    [self.textView setVerticallyResizable:YES];
    [self.textView setHorizontallyResizable:NO];
    [self.textView setAutoresizingMask:NSViewWidthSizable];
    [[self.textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
    [[self.textView textContainer] setWidthTracksTextView:YES];
    
    [self.scrollView setDocumentView:self.textView];
    [self addSubview:self.scrollView];

}

- (void)logAddContent:(NSString *)content{
    NSString *str = [NSString stringWithFormat:@"[ %@ ]: %@\n",[self getCurrentTimes],content];
    
    
    [[self.textView textStorage] appendAttributedString:[[NSAttributedString alloc] initWithString:str]];
    [self.textView scrollRangeToVisible:NSMakeRange([[self.textView string] length], 0)];

}

- (NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    
    return currentTimeString;
    
}
@end
